home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / front.lha / front / src / input.rex < prev    next >
OS/2 REXX Batch file  |  1992-08-18  |  10KB  |  399 lines

  1. /* $Id: input.rex,v 2.3 1992/08/07 15:15:11 grosch rel $ */
  2.  
  3. /* $Log: input.rex,v $
  4.  * Revision 2.3  1992/08/07  15:15:11  grosch
  5.  * allow several scanner and parsers; extend module Errors
  6.  *
  7.  * Revision 2.2  1991/12/04  16:21:41  grosch
  8.  * unified escape conventions for all tools
  9.  *
  10.  * Revision 2.1  1991/11/21  14:47:50  grosch
  11.  * new version of RCS on SPARC
  12.  *
  13.  * Revision 2.0  91/03/08  18:26:49  grosch
  14.  * turned tables into initialized arrays (in C)
  15.  * moved mapping tokens -> strings from Errors to Parser
  16.  * changed interface for source position
  17.  * 
  18.  * Revision 1.3  90/06/11  18:47:51  grosch
  19.  * allow keywords as token names, layout improvements
  20.  * 
  21.  * Revision 1.2  88/12/16  15:03:42  vielsack
  22.  * identifier may now start with backslash ('\') which isn't significant
  23.  * this can be used to define identifiers which are keywords
  24.  * 
  25.  * Revision 1.1  88/10/20  08:51:24  vielsack
  26.  * use tScanAttribute instead of tAttribute
  27.  * 
  28.  * Revision 1.0  88/10/04  14:27:33  vielsack
  29.  * Initial revision
  30.  * 
  31.  */
  32.  
  33. EXPORT    {
  34.  
  35. FROM StringMem    IMPORT tStringRef;
  36. FROM Idents    IMPORT tIdent;
  37. FROM Rules      IMPORT Expression;
  38. FROM Lists      IMPORT tList;
  39. FROM Positions    IMPORT tPosition;
  40.  
  41. TYPE
  42.    AttributeMode = (Keys, Comment, Number, Action, Symbol, Coding, PrioPart, RightSide, Empty);
  43.  
  44.    tScanAttribute=
  45.       RECORD
  46.      Position: tPosition;
  47.      CASE Mode: AttributeMode OF
  48.      | Comment: Comm    : tList;
  49.      | Number: Value    : CARDINAL;
  50.      | Action: Act    : tList;
  51.      | Symbol: Sym    : tIdent;
  52.      | Coding:
  53.          HasCoding    : BOOLEAN ;
  54.          CodNumbPos    : tPosition;
  55.          CodValue    : CARDINAL;
  56.      | PrioPart,
  57.        RightSide:
  58.          Expr:  Expression;
  59.          CASE HasPrio: BOOLEAN OF
  60.            TRUE:
  61.          PriSym    : tIdent;
  62.          PriSymPos    : tPosition;
  63.          END;
  64.      END;
  65.       END;
  66.  
  67. PROCEDURE ErrorAttribute (Token: INTEGER; VAR Attr: tScanAttribute);
  68. }
  69.  
  70. GLOBAL    {
  71.  
  72. FROM SYSTEM    IMPORT ADR, ADDRESS;
  73. FROM Strings    IMPORT tString, ArrayToString, Concatenate, SubString, AssignEmpty, Assign, Char, Length;
  74. FROM StringMem    IMPORT tStringRef, PutString;
  75. FROM Idents    IMPORT MakeIdent;
  76. FROM Lists    IMPORT tList, Append, MakeList;
  77. FROM Limits    IMPORT MaxShortCard;
  78. FROM WriteTok    IMPORT tLanguage, Language;
  79. FROM Errors    IMPORT eWarning, eError, eCharacter, eString, ErrorMessage, ErrorMessageI;
  80.  
  81. CONST
  82.   eCharIgnored    = 10;
  83.   eEolString    = 11;
  84.   eUnClAction    = 12;
  85.   eUnClComment    = 13;
  86.   eUnClString    = 14;
  87.  
  88.   SymEqual    =  1;
  89.   SymColon    =  2;
  90.   SymPoint    =  3;
  91.   SymOr        =  4;
  92.   SymStar    =  5;
  93.   SymPlus    =  6;
  94.   SymList    =  7;
  95.   SymLBrace    =  8;
  96.   SymRBrace    =  9;
  97.   SymLBracket    = 10;
  98.   SymRBracket    = 11;
  99.   SymEXPORT    = 12;
  100.   SymGLOBAL    = 13;
  101.   SymLOCAL    = 14;
  102.   SymBEGIN    = 15;
  103.   SymCLOSE    = 16;
  104.   SymTOKEN    = 17;
  105.   SymOPER    = 18;
  106.   SymNONE    = 19;
  107.   SymLEFT    = 20;
  108.   SymRIGHT    = 21;
  109.   SymRULE    = 22;
  110.   SymPREC    = 23;
  111.   SymComment    = 24; (* Schreibweise wie in MODULA-2        *)
  112.   SymNumber    = 25; (* vorzeichenlose ganze Zahl        *)
  113.   SymAction    = 26; (* in '{' und '}' eingeschlossene Aktion    *)
  114.   SymIdentifier    = 27; (* letter (digit | letter) *        *)
  115.   SymString    = 28; (* durch "'" oder '"' begrenzte Zeichenkette *)
  116.   SymScanner    = 29;
  117.   SymParser    = 30;
  118.  
  119. VAR level: INTEGER;
  120.  
  121. PROCEDURE ErrorAttribute (Token: INTEGER; VAR Attr: tScanAttribute);
  122.   VAR s : tString;
  123.   BEGIN
  124.     ArrayToString ('-error-', s);
  125.     CASE Token OF
  126.       0: ;
  127.     | 1..23:  Attr.Mode    := Keys;
  128.     |      24:  Attr.Mode    := Comment;
  129.           MakeList (Attr.Comm);
  130.     |    25:  Attr.Mode    := Number;
  131.           Attr.Value:= MaxShortCard;
  132.     |    26:  Attr.Mode    := Action;
  133.           MakeList (Attr.Act);
  134.     | 27,28:  Attr.Mode    := Symbol;
  135.           Attr.Sym    := MakeIdent (s);
  136.     END;
  137.   END ErrorAttribute;
  138.  
  139. PROCEDURE GetNumber (VAR Word: tString): CARDINAL;
  140.   VAR i, n, d : CARDINAL;
  141.    BEGIN
  142.       i := 0;
  143.       n := 0;
  144.       WHILE i < Length (Word) DO
  145.     i := i+1;
  146.     d := (ORD(Char (Word, i))-ORD('0'));
  147.     IF (n > MaxShortCard DIV 10) OR (n*10 > MaxShortCard - d) THEN
  148.       RETURN MaxShortCard;
  149.     END;
  150.     n := n*10 + d;
  151.       END;
  152.       RETURN n;
  153.    END GetNumber;
  154. }
  155.  
  156. LOCAL    { VAR Word, String, LastWord: tString; c: CHAR; PrevState: SHORTCARD; }
  157.  
  158. EOF    {
  159. CASE yyStartState OF
  160. | code            : ErrorMessage (eUnClAction , eError, Attribute.Position);
  161. | comment, comment2    : ErrorMessage (eUnClComment, eError, Attribute.Position);
  162. | Str1, Str2, CStr1, CStr2:ErrorMessage (eUnClString, eError, Attribute.Position);
  163. ELSE
  164. END;
  165. }
  166.  
  167. DEFINE
  168.   digit        = {0-9}        .
  169.   letter    = {a-z A-Z _}    .
  170.   cmt        = - {*(\t\n}    .
  171.   cmt2        = - {*\t\n}    .
  172.   codechar    = - {\\{\}\t\n'"} .
  173.   StrCh1    = - {'\t\n}    .
  174.   StrCh2    = - {"\t\n}    .
  175.   CStrCh1    = - {'\t\n\\}    .
  176.   CStrCh2    = - {"\t\n\\}    .
  177.  
  178. START token, rule, code, Str1, Str2, CStr1, CStr2, comment, comment2
  179.  
  180. RULES
  181.  
  182. #token#    "="    : { Attribute.Mode := Keys; RETURN SymEqual    ; }
  183. #rule#    ":"    : { Attribute.Mode := Keys; RETURN SymColon    ; }
  184. #rule#    "."    : { Attribute.Mode := Keys; RETURN SymPoint    ; }
  185. #rule#    "|"    : { Attribute.Mode := Keys; RETURN SymOr    ; }
  186. #rule#    "*"    : { Attribute.Mode := Keys; RETURN SymStar    ; }
  187. #rule#    "+"    : { Attribute.Mode := Keys; RETURN SymPlus    ; }
  188. #rule#    "||"    : { Attribute.Mode := Keys; RETURN SymList    ; }
  189. #rule#    "("    : { Attribute.Mode := Keys; RETURN SymLBrace    ; }
  190. #rule#    ")"    : { Attribute.Mode := Keys; RETURN SymRBrace    ; }
  191. #rule#    "["    : { Attribute.Mode := Keys; RETURN SymLBracket    ; }
  192. #rule#    "]"    : { Attribute.Mode := Keys; RETURN SymRBracket    ; }
  193. #STD#    \SCANNER: { Attribute.Mode := Keys; RETURN SymScanner    ; }
  194. #STD#    \PARSER    : { Attribute.Mode := Keys; RETURN SymParser    ; }
  195. #STD#    \EXPORT    : { Attribute.Mode := Keys; RETURN SymEXPORT    ; }
  196. #STD#    \GLOBAL    : { Attribute.Mode := Keys; RETURN SymGLOBAL    ; }
  197. #STD#    \LOCAL    : { Attribute.Mode := Keys; RETURN SymLOCAL    ; }
  198. #STD#    \BEGIN    : { Attribute.Mode := Keys; RETURN SymBEGIN    ; }
  199. #STD#    \CLOSE    : { Attribute.Mode := Keys; RETURN SymCLOSE    ; }
  200. #STD#    TOKEN    : { Attribute.Mode := Keys; yyStart (token); RETURN SymTOKEN    ; }
  201.  
  202. #token#    OPER    : { Attribute.Mode := Keys; RETURN SymOPER    ; }
  203. #token#    NONE    : { Attribute.Mode := Keys; RETURN SymNONE    ; }
  204. #token#    LEFT    : { Attribute.Mode := Keys; RETURN SymLEFT    ; }
  205. #token#    RIGHT    : { Attribute.Mode := Keys; RETURN SymRIGHT    ; }
  206. #token#    RULE    : { Attribute.Mode := Keys; yyStart (rule); RETURN SymRULE    ; }
  207.  
  208. #rule#    PREC    : { Attribute.Mode := Keys; RETURN SymPREC    ; }
  209.  
  210. #token#    digit +    :
  211. {
  212.   Attribute.Mode := Number;
  213.   GetWord (Word);
  214.   Attribute.Sym := MakeIdent (Word);
  215.   Attribute.Value := GetNumber (Word);
  216.   RETURN SymNumber;
  217. }
  218.  
  219. #STD, token, rule#    \\ letter (letter | digit) *    :
  220. {
  221.   Attribute.Mode := Keys;
  222.   GetWord (Word);
  223.   SubString (Word, 2, Length (Word), String);
  224.   Attribute.Sym := MakeIdent (String);
  225.   RETURN SymIdentifier;
  226. }
  227.  
  228. #STD, token, rule#    letter (letter | digit) *    :
  229. {
  230.   Attribute.Mode := Keys;
  231.   GetWord (Word);
  232.   Attribute.Sym := MakeIdent (Word);
  233.   RETURN SymIdentifier;
  234. }
  235.  
  236. #STD, rule#    "{"    :
  237. {
  238.   PrevState := yyStartState;
  239.   yyStart (code);
  240.   GetWord (LastWord);
  241.   Attribute.Mode := Action;
  242.   MakeList (Attribute.Act);
  243.   level := 1;
  244. }
  245.  
  246. #STD, token, rule# \f | \r :- {}
  247.  
  248. #token, rule, code# '    : { GetWord (String);
  249.                   Attribute.Mode := Symbol;
  250.                 IF Language = C
  251.                 THEN yyStart (CStr1);
  252.                 ELSE yyStart (Str1);
  253.                 END;}
  254.  
  255. #token, rule, code# \"    : { GetWord (String);
  256.                   Attribute.Mode := Symbol;
  257.                 IF Language = C
  258.                 THEN yyStart (CStr2);
  259.                 ELSE yyStart (Str2);
  260.                 END;}
  261.  
  262. #Str1#    StrCh1  + | \'\'    ,
  263. #Str2#    StrCh2  + | \"\"    ,
  264. #CStr1#    CStrCh1 + | \\ ANY ?    ,
  265. #CStr2#    CStrCh2 + | \\ ANY ? :- {GetWord (Word); Concatenate (String, Word);}
  266.  
  267. #CStr1, CStr2#    \\ \n    :- {GetWord (Word); Concatenate (String, Word); yyEol (0);}
  268.  
  269. #Str1, CStr1# '        ,
  270. #Str2, CStr2# \"    :- {Strings.Append (String, Char (String, 1));
  271.                 yyPrevious;
  272.                 IF yyStartState = code THEN
  273.                    Concatenate (LastWord, String);
  274.                 ELSE
  275.                      Attribute.Sym := MakeIdent (String);
  276.                      RETURN SymString;
  277.                 END;}
  278.  
  279. #Str1, Str2, CStr1, CStr2# \t :- {Strings.Append (String, 11C); yyTab;}
  280.  
  281. #Str1, Str2, CStr1, CStr2# \n : {ErrorMessage (eEolString, eError, Attribute.Position);
  282.                 Strings.Append (String, Char (String, 1));
  283.                 yyEol (0); yyPrevious;
  284.                 IF yyStartState = code THEN
  285.                    Concatenate (LastWord, String);
  286.                 ELSE
  287.                      Attribute.Sym := MakeIdent (String);
  288.                    RETURN SymString;
  289.                 END;}
  290.  
  291. #STD, token, rule#    "(*"    :
  292. {
  293.   yyStart (comment);
  294.   GetWord (LastWord);
  295.   Attribute.Mode := Comment;
  296.   MakeList (Attribute.Comm);
  297.   level := 1;
  298. }
  299.  
  300. #STD, token, rule#    "/*"    :- { yyStart (comment2); }
  301.  
  302. #STD, token, rule#    - {\t\n\ }    :
  303. {
  304.   GetWord (Word);
  305.   c := Char (Word, 1);
  306.   ErrorMessageI (eCharIgnored, eWarning, Attribute.Position, eCharacter, ADR (c));
  307. }
  308.  
  309. #code#    \\ ANY | codechar +    :-
  310. {
  311.   GetWord (Word);
  312.   Concatenate (LastWord, Word);
  313. }
  314.  
  315. #code#    \\    :
  316. {
  317.   c := '\';
  318.   ErrorMessageI (eCharIgnored, eWarning, Attribute.Position, eCharacter, ADR (c));
  319. }
  320.  
  321. #code#    \t    :-
  322. {
  323.   yyTab;
  324.   GetWord (Word);
  325.   Concatenate (LastWord, Word);
  326. }
  327.  
  328. #code#    \n    :-
  329. {
  330.   GetWord (Word);
  331.   Concatenate (LastWord, Word);
  332.   Append (Attribute.Act, ADDRESS (PutString (LastWord)));
  333.   AssignEmpty (LastWord);
  334.   yyEol (0);
  335. }
  336.  
  337. #code#    "{"    :-
  338. {
  339.   GetWord (Word);
  340.   Concatenate (LastWord, Word);
  341.   INC (level);
  342. }
  343.  
  344. #code#    "}"    :-
  345. {
  346.   DEC (level);
  347.   GetWord (Word);
  348.   Concatenate (LastWord, Word);
  349.   IF level = 0 THEN
  350.     yyStart (PrevState);
  351.     Append (Attribute.Act, ADDRESS (PutString (LastWord)));
  352.     RETURN SymAction;
  353.   END;
  354. }
  355.  
  356. #comment#    "(*"    :-
  357. {
  358.   GetWord (Word);
  359.   Concatenate (LastWord, Word);
  360.   INC (level);
  361. }
  362.  
  363. #comment#    "*)"    :-
  364. {
  365.   DEC (level);
  366.   GetWord (Word);
  367.   Concatenate (LastWord, Word);
  368.   IF level = 0 THEN
  369.     yyPrevious;
  370.     Append (Attribute.Comm, ADDRESS (PutString (LastWord)));
  371.     RETURN SymComment;
  372.   END;
  373. }
  374.  
  375. #comment#    "(" | "*" | cmt +    :-
  376. {
  377.   GetWord (Word);
  378.   Concatenate (LastWord, Word);
  379. }
  380.  
  381. #comment#    \t    :-
  382. {
  383.   yyTab;
  384.   GetWord (Word);
  385.   Concatenate (LastWord, Word);
  386. }
  387.  
  388. #comment#    \n    :-
  389. {
  390.   GetWord (Word);
  391.   Concatenate (LastWord, Word);
  392.   Append (Attribute.Comm, ADDRESS (PutString (LastWord)));
  393.   AssignEmpty (LastWord);
  394.   yyEol (0);
  395. }
  396.  
  397. #comment2#    "*" | cmt2 +    :- { }
  398. #comment2#    "*/"        :- { yyPrevious; }
  399.